/**
 * A command line interface class for the QualitySoft ContactList
 */
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.*;

/**
 * @author Joshua Zeiser, MS
 * 
 */
public class CLInterface {

	/**
	 * @param Creates
	 *            a ContactList object, prints the menu, and allows the user to
	 *            select a menu option.
	 * @author MS
	 */
	public static void main(String[] args) {
		// uses readFromDisk to check for pre-existing list to load, otherwise
		// returns new empty ContactsList
		ContactsList c1 = readFromDisk(); // JZ
		//String searchLastName;
		int menuSelection = printMenuToConsole();
		while (menuSelection != 4) {
			switch (menuSelection) {
			case 1:
				System.out.println("\nContact List > Add New Contact\n");
				c1.addContact(requestContact());
				break;
			case 2:
				System.out.println("\nContact List > Print List\n");
				System.out.print(c1.toString());
				break;
			case 3:
				System.out.println("\nContact List > Search by Last Name\n");
				ContactsList tList = c1.retrieveContact(searchByLastName());
				if (tList.size() != 0)
					System.out.println(tList);
				else
					System.out.println("\nNo contact with that last name was found.");
				break;
			default:
				System.out.println("\nInvalid Entry. Please Try Again.");
				break;
			}
			menuSelection = reprintMenuToConsole();
		}
		// writes the list back to disk before exiting
		writeToDisk(c1); // JZ
	}
		
	/**
	 * asks user for last name to search for
	 * 
	 * @author MS, Soyoung
	 */
	public static String searchByLastName() {
		Scanner myScanner = new Scanner(System.in);
		System.out.print("Enter Last Name to Search: ");
		String searchName = new String();
		searchName=myScanner.next();
		return searchName;
	}
	/**
	 * calls all request methods for objects comprising a Contact object and uses input to construct Contact object
	 * 
	 * @return Contact
	 * @author MS
	 */
	public static Contact requestContact() {
		Name tName = requestName();
		StreetAddress streetAddress;
		EMail tEMail;
		PhoneNum tPhoneNum;
		Notes tNotes;
		if (tName.getlName().equals("")) {
			streetAddress = new StreetAddress();
			tEMail = new EMail();
			tPhoneNum = new PhoneNum();
			tNotes = new Notes();
		} else {
			streetAddress = requestStreetAddress();
			tEMail = requestEMail();
			tPhoneNum = requestPhoneNum();
			tNotes = requestNotes();
		}
		Contact tContact = new Contact(tName, tNotes, tEMail, tPhoneNum,
				streetAddress);
		return tContact;
	}

	/**
	 * get the Name info from the user and create object of class Name
	 * 
	 * @return Name
	 * @author MS, Soyoung
	 */
	public static Name requestName() {
		Scanner myScanner = new Scanner(System.in);
		System.out.print("First Name: ");
		String fName = new String();
		fName = myScanner.nextLine();
		System.out.print("Last Name (Required): ");
		String lName = new String();
		lName = myScanner.nextLine();
		Name tName = new Name();
		tName.setlName(lName);
		tName.setfName(fName);
		if (lName.equals("")) {
			System.out.println("Please enter a valid last name.");
			System.out.print("Last Name (Required): ");
			lName = myScanner.nextLine();
			tName.setlName(lName);
		}
		return tName;
	}

	/**
	 * get the Notes info from the user and creates object of class Notes
	 * 
	 * @return Notes
	 * @author MS, Soyoung
	 */
	public static Notes requestNotes() {
		Scanner myScanner = new Scanner(System.in);
		System.out.print("Notes: ");
		String notes = new String();
		notes = myScanner.nextLine();
		Notes tNotes = new Notes();
		tNotes.setNote(notes);
		return tNotes;
	}

	/**
	 * get the PhoneNum info from the user and creates object of class PhoneNum
	 * 
	 * @return PhoneNum
	 * @author MS, Soyoung
	 */
	public static PhoneNum requestPhoneNum() {
		Scanner myScanner = new Scanner(System.in);
		System.out.print("Phone Number (no dashes): ");
		String phoneNum = new String();
		phoneNum = myScanner.nextLine();
		PhoneNum tPhoneNum = new PhoneNum();
		tPhoneNum.setPhoneNumber(phoneNum);
		return tPhoneNum;
	}

	/**
	 * get the StreetAddress info from the user and creates object of class StreetAddress
	 * 
	 * @return StreetAddress
	 * @author MS, Soyoung
	 */
	public static StreetAddress requestStreetAddress() {
		Scanner myScanner = new Scanner(System.in);
		System.out.print("Street Address: ");
		String house = new String();
		house = myScanner.nextLine();
		System.out.print("City: ");
		String city = new String();
		city = myScanner.nextLine();
		System.out.print("State: ");
		String state = new String();
		state = myScanner.nextLine();
		System.out.print("ZIP: ");
		String zip = new String();
		zip = myScanner.nextLine();
		System.out.print("Country: ");
		String country = new String();
		country = myScanner.nextLine();
		StreetAddress streetAddress = new StreetAddress();
		streetAddress.setHouse(house);
		streetAddress.setCity(city);
		streetAddress.setState(state);
		streetAddress.setCountry(country);
		streetAddress.setZip(zip);
		return streetAddress;

	}

	/**
	 * get the EMail info from the user and creates object of class EMail
	 * 
	 * @return EMail
	 * @author MS, Soyoung
	 */
	public static EMail requestEMail() {
		Scanner myScanner = new Scanner(System.in);
		System.out.print("Email Address: ");
		String eMail = new String();
		eMail = myScanner.nextLine();
		EMail tEMail = new EMail();
		tEMail.seteMailAddress(eMail);
		return tEMail;
	}

	/**
	 * prints the menu to the console
	 * 
	 * @author MS
	 */
	public static int printMenuToConsole() {
		Scanner myScanner = new Scanner(System.in);
		System.out.print("Contact List Menu\n" + "1 - Add New Contact \n"
				+ "2 - Print Contact List \n" + "3 - Search by Last Name \n"
				+ "4 - Exit Program \n"
				+ "Enter the number of the menu item you wish to access: ");
		int menuSelection = myScanner.nextInt();
		return menuSelection;
	}

	/**
	 * reprints the menu to the console
	 * 
	 * @author MS
	 */
	public static int reprintMenuToConsole() {
		System.out.print("\n");
		return printMenuToConsole();
	}
	


	/**
	 * Reads a ContactsList from disk if it exists
	 * 
	 * @return ContactsList object
	 * @author Josh Zeiser
	 */
	public static ContactsList readFromDisk() {
		ContactsList c = new ContactsList();

		FileInputStream inFile;
		ObjectInputStream inObject;
		try {
			inFile = new FileInputStream("data");
			inObject = new ObjectInputStream(inFile);
			c = (ContactsList) inObject.readObject();
			inFile.close();
			inObject.close();
		} catch (IOException ioe) {
			//System.out.println("No previous Contact List found, starting a new list. " + ioe.getMessage());
		} catch (ClassNotFoundException cnfe) {
			System.out.println("Error casting to ContactsList, "
					+ cnfe.getMessage());
			// } finally {
			// System.out.println("We'll see what happens.");
		}

		return c;
	}

	/**
	 * Writes a ContactsList object to disk
	 * 
	 * @param c
	 *            ContactsList object
	 * @author Josh Zeiser
	 */
	public static void writeToDisk(ContactsList c) {
		FileOutputStream outFile;
		ObjectOutputStream outObject;
		try {
			outFile = new FileOutputStream("data");
			outObject = new ObjectOutputStream(outFile);
			outObject.writeObject(c);
			outFile.close();
			outObject.close();
			System.out.println("Your contacts have been saved.");
		} catch (IOException ioe) {
			System.out.println("Error writing Contact List to disk, "
					+ ioe.getMessage());
		} 
	}
}
/* Run from console ------------------------------------------------------------------------------
 * Contact List Menu
1 - Add New Contact 
2 - Print Contact List 
3 - Search by Last Name 
4 - Exit Program 
Enter the number of the menu item you wish to access: 1

Contact List > Add New Contact

First Name: Sally
Last Name (Required): Jones
Street Address: 100 Sample Street
City: Mountain View
State: CA
ZIP: 94043
Country: USA
Email Address: sally@gmail.com
Phone Number (no dashes): 0001234567
Notes: Sample notes here.

Contact List Menu
1 - Add New Contact 
2 - Print Contact List 
3 - Search by Last Name 
4 - Exit Program 
Enter the number of the menu item you wish to access: 1

Contact List > Add New Contact

First Name: Warren
Last Name (Required): Zevon
Street Address: 1 Werewolf Lane
City: London
State: TX
ZIP: 75009
Country: USA
Email Address: warrenz@gmail.com
Phone Number (no dashes): 11232343456
Notes: Warren's notes.

Contact List Menu
1 - Add New Contact 
2 - Print Contact List 
3 - Search by Last Name 
4 - Exit Program 
Enter the number of the menu item you wish to access: 1

Contact List > Add New Contact

First Name: Tom
Last Name (Required): Cruise
Street Address: 894 Star Street
City: Hollywood
State: CA
ZIP: 94089
Country: USA
Email Address: tom@gmail.com
Phone Number (no dashes): 0980989876
Notes: Tom's notes.

Contact List Menu
1 - Add New Contact 
2 - Print Contact List 
3 - Search by Last Name 
4 - Exit Program 
Enter the number of the menu item you wish to access: 2

Contact List > Print List


Tom Cruise
894 Star Street
Hollywood, CA 94089
USA
tom@gmail.com
098-098-9876
Tom's notes.

Sally Jones
100 Sample Street
Mountain View, CA 94043
USA
sally@gmail.com
000-123-4567
Sample notes here.

Warren Zevon
1 Werewolf Lane
London, TX 75009
USA
warrenz@gmail.com
1-123-234-3456
Warren's notes.

Contact List Menu
1 - Add New Contact 
2 - Print Contact List 
3 - Search by Last Name 
4 - Exit Program 
Enter the number of the menu item you wish to access: 3

Contact List > Search by Last Name

Enter Last Name to Search: Jones

Sally Jones
100 Sample Street
Mountain View, CA 94043
USA
sally@gmail.com
000-123-4567
Sample notes here.


Contact List Menu
1 - Add New Contact 
2 - Print Contact List 
3 - Search by Last Name 
4 - Exit Program 
Enter the number of the menu item you wish to access: 3

Contact List > Search by Last Name

Enter Last Name to Search: Ortega

No contact with that last name was found.

Contact List Menu
1 - Add New Contact 
2 - Print Contact List 
3 - Search by Last Name 
4 - Exit Program 
Enter the number of the menu item you wish to access: 5

Invalid Entry. Please Try Again.

Contact List Menu
1 - Add New Contact 
2 - Print Contact List 
3 - Search by Last Name 
4 - Exit Program 
Enter the number of the menu item you wish to access: 1

Contact List > Add New Contact

First Name: George
Last Name (Required): 
Please enter a valid last name.
Last Name (Required): Jones
Street Address: 101 Sample Street
City: Mountain View
State: CA
ZIP: 94043
Country: USA
Email Address: georgej@gmail.com
Phone Number (no dashes): 001234567
Notes: Sample notes here.

Contact List Menu
1 - Add New Contact 
2 - Print Contact List 
3 - Search by Last Name 
4 - Exit Program 
Enter the number of the menu item you wish to access: 1

Contact List > Add New Contact

First Name: Madonna
Last Name (Required): 
Please enter a valid last name.
Last Name (Required): 
The contact was not added because no last name was entered.

Contact List Menu
1 - Add New Contact 
2 - Print Contact List 
3 - Search by Last Name 
4 - Exit Program 
Enter the number of the menu item you wish to access: 4
Your contacts have been saved.

//NEW SESSION
Contact List Menu
1 - Add New Contact 
2 - Print Contact List 
3 - Search by Last Name 
4 - Exit Program 
Enter the number of the menu item you wish to access: 3

Contact List > Search by Last Name

Enter Last Name to Search: Jones

Sally Jones
100 Sample Street
Mountain View, CA 94043
USA
sally@gmail.com
000-123-4567
Sample notes here.

George Jones
101 Sample Street
Mountain View, CA 94043
USA
georgej@gmail.com
001234567
Sample notes here.


Contact List Menu
1 - Add New Contact 
2 - Print Contact List 
3 - Search by Last Name 
4 - Exit Program 
Enter the number of the menu item you wish to access: 
 */